package com.itheima.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.QuestionItem;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ymsxyz
 * @version 1.0
 * @description 注意:因为选项简单,增删查整合再一个页面list.jsp,通过toEdit/
 * @date 2021/2/7 10:00
 */
@WebServlet("/store/questionItem")
public class QuestionItemServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        if ("list2".equals(operation)) {
            this.list2(request, response);
        } else if ("save".equals(operation)) {
            this.save(request, response);
        } else if ("toEdit".equals(operation)) {
            this.toEdit(request, response);
        } else if ("edit".equals(operation)) {
            this.edit(request, response);
        } else if ("delete".equals(operation)) {
            this.delete(request, response);
        }
    }

    /**
     * @param: request
     * @param: response
     * @description: 根据问题id查询所有选项  废弃
     * @return: void
     * @author: niaonao
     * @date: 2021/2/6
     */

    /*private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //需传给
        String questionId = request.getParameter("questionId");
        PageInfo all = questionItemService.findAll(questionId, 1, 100);
        //将数据保存到指定的位置  注意:刚开始all.list=null,该问题没有对应的选项,将questionId传递供增加对应选项
        request.setAttribute("questionId", questionId);
        request.setAttribute("page", all);
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/store/questionItem/list.jsp").forward(request, response);
    }*/

    /**
     * @param: request
     * @param: response
     * @description: 默认为保存,所有操作结束都请求到list2
     * @return: void
     * @author: niaonao
     * @date: 2021/2/7
     */
    private void list2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String questionId = request.getParameter("questionId");
        //进入list页时添加对应的问题id，为添加操作使用
        request.setAttribute("questionId", questionId);
        //获取数据
        PageInfo all = questionItemService.findAll(questionId, 1, 100);
        //将数据保存到指定的位置
        request.setAttribute("page", all);

        if (request.getAttribute("operation") == null) {
            //保存一个操作的类型，传递到前端save
            request.setAttribute("operation", "save");
        }

        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/store/questionItem/list.jsp").forward(request, response);
    }

    /**
     * @param: request
     * @param: response
     * @description: 新增选项
     * @return: void
     * @author: niaonao
     * @date: 2021/2/6
     */
    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将数据获取到，封装成一个对象
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class, "yyyy-MM-dd");
        //调用业务层接口save
        questionItemService.save(questionItem);
        //跳转回到页面list,显示当前问题所有选项
        response.sendRedirect(request.getContextPath() + "/store/questionItem?operation=list2&questionId=" + questionItem.getQuestionId());
    }

    /**
     * @param: request
     * @param: response
     * @description: 编辑用list方法跳转, 传递操作类型edit到前端save按钮
     * @return: void
     * @author: niaonao
     * @date: 2021/2/6
     */
    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询要修改的数据findById
        String id = request.getParameter("id");
        QuestionItem questionItem = questionItemService.findById(id);
        //将数据加载到指定区域，供页面获取
        request.setAttribute("questionItem", questionItem);
        //保存一个操作的类型，传递到前端save
        request.setAttribute("operation", "edit");
        //跳转回到页面list
        list2(request, response);
    }

    /**
     * @param: request
     * @param: response
     * @description: 编辑
     * @return: void
     * @author: niaonao
     * @date: 2021/2/7
     */
    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //将数据获取到，封装成一个对象
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class, "yyyy-MM-dd");
        //调用业务层接口save
        questionItemService.update(questionItem);
        //跳转回到页面list
        response.sendRedirect(request.getContextPath() + "/store/questionItem?operation=list2&questionId=" + questionItem.getQuestionId());
    }

    /**
     * @param: request
     * @param: response
     * @description: 删除一条选项
     * @return: void
     * @author: niaonao
     * @date: 2021/2/6
     */
    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //将数据获取到，封装成一个对象 注意:需要问题id来刷新当前页面,更符合逻辑
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class);
        //调用业务层接口save
        questionItemService.delete(questionItem);
        //跳转回到页面list 注意:跳到当前问题选项页,请求加上问题id参数
        response.sendRedirect(request.getContextPath() + "/store/questionItem?operation=list2&questionId="
                + questionItem.getQuestionId());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}